package com.blast.comicsisland.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.blast.comicsisland.R;
import com.blast.comicsisland.model.game.gamebody.Info;
import com.blast.comicsisland.utils.ImageLoaderUtil;

import java.util.List;

/**
 * Created by Android Studio
 * Author: hg
 * Date: 15/6/26
 * Email: 531057892@qq.com
 */
public class BookstoreGameAdapter extends BaseAdapter {

    private Context context;
    private List<Info> infos;
    private ImageLoaderUtil imageLoaderUtil;
    private ImageLoader imageLoader;

    public BookstoreGameAdapter(Context context, List<Info> infos) {
        this.context = context;
        this.infos = infos;
        imageLoaderUtil = ImageLoaderUtil.getInstance();
        imageLoader = imageLoaderUtil.getImageLoader();
    }

    @Override
    public int getCount() {
        int ret = 0;
        if (infos != null) {
            ret = infos.size();
        }
        return ret;
    }

    @Override
    public Object getItem(int position) {
        Object ret = null;
        if (ret != null) {
            ret = infos.get(position);
        }
        return ret;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View  ret = null;
        if (convertView != null) {
            ret = convertView;
        }else {
            ret = LayoutInflater.from(context).inflate(R.layout.item_bookstore_game_body,parent,false);
        }

        ViewHolder holder = (ViewHolder) ret.getTag();

        if (holder ==null){
            holder = new ViewHolder();
            holder.networkImageView = (NetworkImageView) ret.findViewById(R.id.item_bookstore_game_network_imageview);
            holder.textView = (TextView) ret.findViewById(R.id.item_bookstore_game_textview);
            ret.setTag(holder);
        }

        if (infos != null) {
            Info info = infos.get(position);
            String promotionimage = info.getPromotionimage();
            String name = info.getName();
            holder.networkImageView.setDefaultImageResId(R.drawable.loading_land);
            holder.networkImageView.setImageUrl(promotionimage,imageLoader);
            holder.textView.setText(name);
        }

        return ret;
    }

    class ViewHolder{
        private NetworkImageView networkImageView;
        private TextView textView;
    }

}
